using System;
using System.IO;
using System.Reflection;
using OpenNetCommerce.Localization;
using MettleSystems.dashCommerce.Store.Web;
using System.Security.Permissions;

namespace MettleSystems.dashCommerce.Web.admin
{
	public partial class about : MettleSystems.dashCommerce.Controls.AdminPage
	{

		#region Page Events

		/// <summary>
		/// Handles the Load event of the Page control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			lblVersion.Text = string.Format(LocalizationUtility.GetText("lblVersionNumber"),
			                                System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
			txtLicenseAgreement.Text = File.ReadAllText(Server.MapPath("~/license.txt"));
			//this.Title = LocalizationUtility.GetText("titleAbout");
			BindAssemblies();
		}


		#endregion

		#region Methods

		#region Private

		/// <summary>
		/// Gets the application assemblies.
		/// </summary>
		/// <returns></returns>
		private Assembly[] GetApplicationAssemblies()
		{
			string[] files = Directory.GetFiles(Server.MapPath("~/bin"), "*.dll");
			Assembly[] assemblies = new Assembly[files.Length];
			for (int i = 0; i < files.Length; i++) {
				assemblies[i] = Assembly.ReflectionOnlyLoadFrom(files[i]);
			}
			return assemblies;
		}

		private void BindAssemblies()
		{
			try {
				Assembly[] assemblies = GetApplicationAssemblies();
				dgAssemblies.DataSource = assemblies;
				dgAssemblies.Columns[0].HeaderText = LocalizationUtility.GetText("lblAssemblies");
				dgAssemblies.DataBind();
			}
			catch (TargetInvocationException) {
				//swallow it - probably running in Medium Trust
				lblAssembliesTitle.Visible = false;
				dgAssemblies.Visible = false;
			}
			catch (Exception) {
				//nuthin' to see here, move along.
				lblAssembliesTitle.Visible = false;
				dgAssemblies.Visible = false;
			}
		}


		#endregion

		#endregion

	}
}
